package edu.washington.cs.workflow.recommender;

import java.util.LinkedList;
import java.util.List;

import com.ibm.wala.ipa.callgraph.CGNode;

import edu.washington.cs.workflow.analysis.AccessedGUIObjectFinder;
import edu.washington.cs.workflow.analysis.AnalysisBuilder;
import edu.washington.cs.workflow.analysis.GUIObjectInitFinder;
import edu.washington.cs.workflow.analysis.GUIObjectInitInfo;
import edu.washington.cs.workflow.analysis.AnalysisBuilder.CG;
import edu.washington.cs.workflow.analysis.AnalysisBuilder.UIFramework;
import edu.washington.cs.workflow.matching.CGNodeDifferentior;
import edu.washington.cs.workflow.matching.ExecutionTraceMatcher;
import edu.washington.cs.workflow.util.Utils;
import edu.washington.cs.workflow.util.WALAUtils;

@Deprecated
public class BrokenWorkflowRepairer {

	public List<GUIObjectInitInfo> recommendGUIActions() {
		List<GUIObjectInitInfo> actions = new LinkedList<GUIObjectInitInfo>();
		
		//
		String path1 = "D:\\research\\configurations\\workspace\\workflow-example\\bin\\example\\swing";
		String path2 = "D:\\research\\configurations\\workspace\\workflow-example-1\\bin\\example\\swing";
		String main = "Lexample/swing/HelloWorldFrame";
		String[] appPackages = new String[]{"example.swing"};
		
		AnalysisBuilder oldBuilder = new AnalysisBuilder(path1, main, appPackages);
		oldBuilder.setUIFramework(UIFramework.SWING);
		oldBuilder.setCGType(CG.OneCFA);
		oldBuilder.buildAnalysis();
		
		AnalysisBuilder newBuilder = new AnalysisBuilder(path2, main, appPackages);
		newBuilder.setUIFramework(UIFramework.SWING);
		newBuilder.setCGType(CG.OneCFA);
		newBuilder.buildAnalysis();
		
		String methodSignature = "example.swing.HelloWorldFrame$1.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		
		AccessedGUIObjectFinder oldFinder = new AccessedGUIObjectFinder(oldBuilder);
		AccessedGUIObjectFinder newFinder = new AccessedGUIObjectFinder(newBuilder);
		
		CGNodeDifferentior differ = new CGNodeDifferentior(oldFinder, newFinder);
		
		ExecutionTraceMatcher matcher = new ExecutionTraceMatcher(oldBuilder, newBuilder);
		CGNode newEventHandler = matcher.findMatchedMethodByNameSig(methodSignature);
		
		List<GUIObjectInitInfo> addedObjs = differ.findAddedGUIObjects(methodSignature, methodSignature);
		Utils.debugPrintln("size of added objs: " + addedObjs.size());
		
		GUIObjectInitFinder finder = new GUIObjectInitFinder(newBuilder);
		List<GUIObjectInitInfo> initObjs = finder.findGUIObjects(WALAUtils.getJavaFullClassName(newEventHandler.getMethod().getDeclaringClass()));
		
		actions.addAll(addedObjs);
		actions.addAll(initObjs);
		
		return actions;
	}
	
	public static void main(String[] args) {
		List<GUIObjectInitInfo>  actions = new BrokenWorkflowRepairer().recommendGUIActions();
		
		for(GUIObjectInitInfo action : actions) {
			Utils.debugPrintln(action.getObjectInfo());
		}
	}
}
